package com.accelstack.cmp.service;

import com.accelstack.cmp.entity.ResourceUsage;
import com.accelstack.cmp.repository.ResourceUsageRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * 资源使用监控Service
 */
@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class ResourceUsageService {
    
    private final ResourceUsageRepository resourceUsageRepository;
    
    @Transactional
    public ResourceUsage create(ResourceUsage resourceUsage) {
        return resourceUsageRepository.save(resourceUsage);
    }
    
    @Transactional
    public ResourceUsage update(ResourceUsage resourceUsage) {
        return resourceUsageRepository.save(resourceUsage);
    }
    
    @Transactional
    public void deleteById(Long id) {
        resourceUsageRepository.deleteById(id);
    }
    
    public Optional<ResourceUsage> findById(Long id) {
        return resourceUsageRepository.findById(id);
    }
    
    public List<ResourceUsage> findAll() {
        return resourceUsageRepository.findAll();
    }
    
    public Page<ResourceUsage> findAll(Pageable pageable) {
        return resourceUsageRepository.findAll(pageable);
    }
    
    public List<ResourceUsage> findByVirtHostId(Long virtHostId) {
        return resourceUsageRepository.findByVirtHostId(virtHostId);
    }
    
    public List<ResourceUsage> findByVirtHostIdAndTimeRange(Long virtHostId, Date startTime, Date endTime) {
        return resourceUsageRepository.findByVirtHostIdAndSampleTimeBetween(virtHostId, startTime, endTime);
    }
}

